package mobile.TheOldRepublic;

import java.io.IOException;
import java.io.OptionalDataException;
import java.net.URL;
import java.util.List;

import mobile.TheOldRepublic.util.Constants;

import com.explodingzombie.util.GeneralUtils;
import com.explodingzombie.util.RssItem;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class RssItemAdapter extends BaseAdapter {
	
	private List<RssItem> items;
	private Context context;
	
	public RssItemAdapter(Context context, int textViewResourceId, List<RssItem> items)
		throws OptionalDataException, ClassNotFoundException, IOException
	{
		this.context = context;
		this.items = items;
	}
	
	public int getCount(){
		if(items == null) return 0;
		return items.size();
	}
	
	public Object getItem(int position){
		return items.get(position);
	}
	
	public long getItemId(int position){
		return position;
	}
	
	public View getView(final int pos, View convertView, ViewGroup parent){
		LinearLayout tr = null;
		if(items != null){
			RssItem item = items.get(pos);
				LayoutInflater vi = (LayoutInflater) context.getSystemService(
	                    Context.LAYOUT_INFLATER_SERVICE);
				tr = (LinearLayout)vi.inflate(R.layout.blank_layout, parent, false);
				tr.setGravity(Gravity.CENTER_VERTICAL);
				tr.setMinimumHeight(Constants.FEED_ITEM_MIN_HEIGHT);
				tr.setPadding(0, 0, 0, 0);
//				tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
				
				ImageView img = new ImageView(context);
				tr.addView(img);				
				try{
					URL imgUrl = new URL(item.getThumbnail());
//					URLConnection imgConn = imgUrl.openConnection();
//					InputStream inStream = imgConn.getInputStream();
//					Drawable image = Drawable.createFromStream(inStream, "src");
//				    img.setImageDrawable(image);
					
					ImageDownloader downloader = (ImageDownloader)ImageDownloader.getInstance();
					downloader.addFileToDownload(item.getThumbnail(), img, this);
					if(!downloader.isAlive())
						downloader.start();
				}catch(Exception e){}
				// Background downloading of thumbnail images. LP
				/*ImageDownloader downloader = (ImageDownloader)ImageDownloader.getInstance();
				downloader.addFileToDownload(item.getThumbnail(), img, this);
				if(!downloader.isAlive())
					downloader.start();*/
				
				TextView txt = new TextView(context);
				if(GeneralUtils.isEmpty(item.getSummary()))
					txt.setText(item.getTitle());
				else
					txt.setText(item.getSummary());
				txt.setPadding(5, 1, 5, 1);
				txt.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);				
				tr.addView(txt);
		}
		
		return tr;
	}
	
	public void setImage(ImageView view, Drawable image){
		view.setImageDrawable(image);
	}

}
